package elementos;


import java.io.IOException;
import java.net.URL;

import mapControl.GraphicsMapControl;

import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;
import com.jmex.effects.particles.ParticleMesh;
import com.model.md5.controller.MD5Controller;
import com.model.md5.importer.MD5Importer;
import com.model.md5.interfaces.IMD5Animation;
import com.model.md5.interfaces.IMD5Node;

import explosionFactory.ExplosionFactory;


public class Bomba extends Elemento implements Runnable {

	private float countdown = 3;
	private int potencia;
	private GraphicsMapControl mapControl;
	private boolean live;
	
	 AudioTrack targetSound;
	
	public Bomba(Player player, GraphicsMapControl mapControl) {
		super();
		potencia = mapControl.getBombPower();
		this.mapControl = mapControl;
		setCol(player.getCol());
		setRow(player.getRow());
		
		AudioSystem audio = AudioSystem.getSystem();
		targetSound = audio.createAudioTrack(getClass().getResource(
        "/music/explosion.wav"), false);
		targetSound.setVolume(1.0f);
		
	}

	
	public void detonar()  {
		mapControl.removeBomb(this);
		mapControl.getContadorBombas().detonarBomba();
		live = false;
		targetSound.play();
		
		explosionCentral(mapControl.getTraslationCentrada(row, col));

		boolean ph = true;
		boolean pv = true;

		for (int dist = 1; dist <= potencia; dist++) {
			
			if (ph) {
				ph = mapControl.detonate((row - dist), col);
				ph = mapControl.detonate((row + dist), col);
			}
			if (pv) {
				pv = mapControl.detonate(row, (col + dist));
				pv = mapControl.detonate(row, (col - dist));
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public void run() {
		
		ExplosionFactory.warmup();
		node = mapControl.getPhysicsSpace().createStaticNode();	
		
		String dir = "data/model/bomb/";
		URL md5mesh = getClass().getClassLoader().getResource(
				dir + "mesh.md5Mesh");
		
		URL animIdle = getClass().getClassLoader().getResource(
				dir + "idle.md5anim");
		
		MD5Importer importer = new MD5Importer();
		IMD5Node body = null;
		IMD5Animation idle = null;
		
		try {

			importer.loadMesh(md5mesh, "ModelNode");
			body = importer.getMD5Node();
			
			importer.loadAnim(animIdle, "idle");
			idle = importer.getAnimation();

			
		} catch (IOException e) {
			System.out
					.print("no se cargo bien algun dato del jugador, verificar la existencia y la correctitud de la ruta de los archivos .md5mesh y .md4anim");
		}
		MD5Controller modelController;
		modelController = new MD5Controller(body);
		modelController.addAnimation(idle);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);
		body.addController(modelController);
		Node model = (Node) body;
		
		model.setLocalScale(.03f);
		model.setLocalTranslation(0,1f, 0);

		node.attachChild(model);
		node.setLocalTranslation(mapControl.getTraslationCentrada(row, col));

		mapControl.getContadorBombas().instalarBomba();
		mapControl.addBomb(this);

		mapControl.getRootNode().attachChild(node);
		mapControl.getRootNode().updateRenderState();
		
		
		live = true;
		try {
			while (countdown > 0 && live) {
				Thread.sleep(1000);
				countdown--;
			}
			detonar();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 

	}

	public void explosionCentral(Vector3f pos) {
		ParticleMesh explosion = ExplosionFactory.getExplosion();
		explosion.setOriginOffset(pos.clone());
		explosion.forceRespawn();
		mapControl.getRootNode().attachChild(explosion);
	}

}
